import web
import model
from web.contrib.template import render_jinja

urls=(
        '','index',
        '/','index',
        '/login','login',
        '/addUser','addUser',
        '/loginOut','loginOut',
        '/newPost','newPost',
        '/editPost','editPost',
        '/postList','postList',
        '/manageCateg','manageCateg',
        '/editCateg','editCateg'
        )

render=render_jinja(
        'templates/admin',
        encoding='utf-8',
)       

web.config.debug = False

def session_hook():
	web.ctx.session = web.config._session

app = web.application(urls, globals(), autoreload=False)
application = app.wsgifunc()
app.add_processor(web.loadhook(session_hook))


def checkLogin():
	if web.ctx.session.login == 1: 
		return
	else:
		raise web.seeother('/login')
                        
                
class index:
        def GET(self):
                checkLogin()
                web.header('Content-Type','text/html; charset=UTF-8')
                return render.index()
                
class login:
        def GET(self):
                web.header('Content-Type','text/html; charset=UTF-8')
                return render.login(locals())
        def POST(self):
                web.header('Content-Type','text/html; charset=UTF-8')
                i=web.input()
                username=model.MD5(i.username)
                password=model.MD5(i.password)
                check=model.checkLogin(username,password)
                if check:
                        web.config._session.login=1
                        raise web.seeother('/')
                else:
                        return 'fail'
                
class addUser:
        def GET(self):
                checkLogin()
                web.header('Content-Type','text/html; charset=UTF-8')
                return render.addUser(locals())
        def POST(self):
                checkLogin()
                i=web.input()
                username=model.MD5(i.username)
                password=model.MD5(i.password)
                model.addUser(username,password)
                raise web.seeother('/addUser')

class newPost:
        def GET(self):
                checkLogin()
                web.header('Content-Type','text/html; charset=UTF-8')
                categs=model.getCategs()
                return render.newPost(locals())
        def POST(self):
                checkLogin()
                web.header('Content-Type','text/html; charset=UTF-8')
                i=web.input(mode='1')
                if i.title:
                        model.addPost(i.title,i.content,i.categId,i.mode)
                raise web.seeother('/newPost')

class editPost:
        def GET(self):
                checkLogin()
                i=web.input()
                categs=model.getCategs()
                post=model.getPost(i.id)
                web.header('Content-Type','text/html; charset=UTF-8')
                return render.editPost(locals())
        def POST(self):
                checkLogin()
                web.header('Content-Type','text/html; charset=UTF-8')
                i=web.input()
                submitType=i.submitType
                if submitType=='update' or submitType=='release':
                        model.updatePost(i.id,i.title,i.content,i.categId,1);
                elif submitType=='saveDraft':
                        model.updatePost(i.id,i.title,i.content,i.categId,0);
                return web.seeother('/postList')
                
class postList:
        def GET(self):
                checkLogin()
                web.header('Content-Type','text/html; charset=UTF-8')
                i=web.input(postStatus=None)
                allCount=model.getPostsCount()
                draftCount=model.getPostsCount(0)
                hasPostedCount=model.getPostsCount(1)
                if i.postStatus=='posted':
                        posts=model.getPosts(1)
                elif i.postStatus=='draft':
                        posts=model.getPosts(0)
                else:
                        posts=model.getPosts()
                return render.postList(locals())
                
class manageCateg:
        def GET(self):
                checkLogin()
                web.header('Content-Type','text/html; charset=UTF-8')
                categs=model.getCategs()
                return render.manageCateg(locals())
        def POST(self):
                checkLogin()
                i=web.input(mode='normol',deleteId=[],categName=None)
                if i.mode and i.mode=='delete':
                        model.deleteCateg(i.deleteId)
                if i.categName:
                        model.addCateg(i.categName,i.alias)
                raise web.seeother('/manageCateg')

class editCateg:
        def GET(self):
                checkLogin()
                i=web.input()
                web.header('Content-Type','text/html; charset=UTF-8')
                categ=model.getCateg(i.id)
                return render.editCateg(locals())
        def POST(self):
                checkLogin()
                i=web.input()
                web.header('Content-Type','text/html; charset=UTF-8')
                model.updateCateg(i.id,i.categName,i.alias)
                raise web.seeother('/manageCateg')
                
class loginOut:
        def GET(self):
                checkLogin()
                web.config._session.login=0
                raise web.seeother('/')
if __name__ == "__main__":
    app.run()   
